package com.platform.qujia.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.platform.qujia.dao.DeputyCardRuleMapper;
import com.platform.qujia.dao.ElectronicMembershipCardMapper;
import com.platform.qujia.pojo.CustomerElectronicCard;
import com.platform.qujia.pojo.DeputyCardRule;
import com.platform.qujia.service.DeputyCardRuleService;
import com.platform.qujia.utils.ResultData;
import com.platform.qujia.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

@Service("deputyCardRuleService")
@Slf4j
public class DeputyCardRuleServiceImpl implements DeputyCardRuleService {


    @Autowired
    private DeputyCardRuleMapper deputyCardRuleMapper;

    @Resource
    private ElectronicMembershipCardMapper electronicMembershipCardMapper;


    @Override
    public Long saveDeputyCardRule(DeputyCardRule deputyCardRule) {
        if(Objects.isNull(deputyCardRule) || Objects.isNull(deputyCardRule.getMainId())){
            throw new CustomException("新增副卡规则请求参数异常");
        }
        CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
        customerElectronicCard.setId(deputyCardRule.getMainId().intValue());
        CustomerElectronicCard electronicCardInfo = electronicMembershipCardMapper.getElectronicCardInfo(customerElectronicCard);
        if(Objects.isNull(electronicCardInfo)){
            throw new CustomException("电子卡主卡不存在");
        }
        Long i = deputyCardRuleMapper.insertSelective(deputyCardRule);
        if(i <= 0){
            throw new CustomException("新增副卡规则失败");
        }
        // 返回新增规则id
        return deputyCardRule.getRuleId();
    }

    @Override
    public DeputyCardRule selectDeputyCardRule(DeputyCardRule deputyCardRule) {
        DeputyCardRule result = deputyCardRuleMapper.selectByMainId(deputyCardRule.getMainId());
        CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
        customerElectronicCard.setId(deputyCardRule.getMainId().intValue());
        CustomerElectronicCard electronicCardInfo = electronicMembershipCardMapper.getElectronicCardInfo(customerElectronicCard);
        if(Objects.isNull(electronicCardInfo)){
            throw new CustomException("电子卡主卡不存在");
        }
        if(Objects.isNull(result)){
            DeputyCardRule rule = new DeputyCardRule();
            rule.setMainAmt((Objects.nonNull(electronicCardInfo.getAmt()) ? electronicCardInfo.getAmt() : BigDecimal.ZERO)
                    .add((Objects.nonNull(electronicCardInfo.getPresentAmt()) ? electronicCardInfo.getPresentAmt() : BigDecimal.ZERO)));
            return rule;
        }
        result.setMainAmt((Objects.nonNull(electronicCardInfo.getAmt()) ? electronicCardInfo.getAmt() : BigDecimal.ZERO)
                .add((Objects.nonNull(electronicCardInfo.getPresentAmt()) ? electronicCardInfo.getPresentAmt() : BigDecimal.ZERO)));
        return result;
    }
}
